package com.project.forlearningdemo.config;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.project.forlearningdemo.pojo.vo.ResponseInfo;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.mail.MailSendException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.mail.MessagingException;
import java.io.IOException;

@RestControllerAdvice("com.project.forlearningdemo")
public class ExceptionConfig{

    @ExceptionHandler(value = MessagingException.class)
    public String messagingException(){
        return JSON.toJSONString(new ResponseInfo<>(405, "邮箱消息发送失败！", false ));
    }

    @ExceptionHandler(value = MailSendException.class)
    public String mailSendException(){
        return JSON.toJSONString(new ResponseInfo<>(404, "找不到，邮箱发送失败！", false));
    }

    @ExceptionHandler(UnknownAccountException.class)
    public String unknownAccountException(){
        return JSON.toJSONString(new ResponseInfo<>(404, "账号不存在！", false));
    }

    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public String incorrectCredentialsException(){
        return JSON.toJSONString(new ResponseInfo<>(405, "密码错误!", false));
    }

    @ExceptionHandler(value = NumberFormatException.class)
    public String numberFormatException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "数字类型出错", false));
    }

    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    public String methodArgumentTypeMismatchException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "接口参数类型不匹配", false));
    }

    @ExceptionHandler(IOException.class)
    public String iOException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "文件上传失败", false));
    }

    @ExceptionHandler(StringIndexOutOfBoundsException.class)
    public String stringIndexOutOfBoundsException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "参数错误", false));
    }

    @ExceptionHandler(ExpiredJwtException.class)
    public String expiredJwtException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "令牌无效", false));
    }

    @ExceptionHandler(JsonProcessingException.class)
    public String jsonProcessingException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "JSON解析失败", false));
    }

    @ExceptionHandler(JSONException.class)
    public String jSONException(){
        return JSON.toJSONString(new ResponseInfo<>(500, "json字符串转化失败", false));
    }

    @ExceptionHandler
    public String exceptionHandler(Exception exception){
        exception.printStackTrace();
        return JSON.toJSONString(new ResponseInfo<>(500, "出现错误了！！！", false));
    }
}
